package lesson.db;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class DbUtil {
    private static SqlSessionFactory factory = null;

    static {
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    /**
     * 获取session，非自动提交事务
     * @return
     */
    public static SqlSession getSession() {

        return factory.openSession();
    }

    /**
     * 获取session
     * @param autoCommit 控制事务是否自动提交
     * @return
     */
    public static SqlSession getSession(boolean autoCommit) {
        return factory.openSession(autoCommit);
    }


}